# load libraries
library(tidyverse)
library(patchwork)
library(here)
library(glue)
library(ggbeeswarm)


# source
source("replication/00-funcs.R")


# read data
disp = read_rds(here("replication", "data", "descriptive-data.rds"))



# Displacement experiences -------------------------------------------------------





## Return

# why return? 
tab = disp %>% 
  mutate(why = case_when(str_detect(dis_returny, "1") ~ "family/friends",
                         str_detect(dis_returny, "2") ~ "security improved",
                         str_detect(dis_returny, "6") ~ "security improved",
                         str_detect(dis_returny, "3") ~ "state helped return",
                         str_detect(dis_returny, "4") ~ "economic necessity",
                         str_detect(dis_returny, "9") ~ "economic necessity",
                         str_detect(dis_returny, "5") ~ "destination worse",
                         str_detect(dis_returny, "7") ~ "cooperated with armed group",
                         str_detect(dis_returny, "8") ~ "recover my land",
                         TRUE ~ "other")) %>% 
  group_by(why) %>% 
  tally() %>% 
  filter(why != "other") %>% 
  mutate(pct = n/sum(n)) %>% 
  mutate(group = "Why did you decide to return?")

p1 = ggplot(tab, aes(x = reorder(why, n), y = pct)) + 
  ggalt::geom_lollipop(size = 1.5, color = "#003049") + 
  coord_flip() + 
  facet_wrap(vars(group)) + 
  labs(y = glue::glue("Percent of respondents\n(N = {sum(tab$n)})"), 
       x= NULL) + 
  hrbrthemes::scale_y_percent(limits = c(0, .5))


# why no return? 
tab = disp %>% 
  mutate(why = case_when(str_detect(dis_noreturn, "1") ~ "property destroyed",
                         str_detect(dis_noreturn, "2|5") ~ "security concerns",
                         str_detect(dis_noreturn, "3") ~ "destination better",
                         str_detect(dis_noreturn, "4|7") ~ "can't recover land",
                         TRUE ~ "other")) %>% 
  group_by(why) %>% 
  tally() %>% 
  filter(why != "other") %>% 
  mutate(pct = n/sum(n)) %>% 
  mutate(group = "Why were you unable\nto return?")


p2 = ggplot(tab, aes(x = reorder(why, n), y = pct)) + 
  ggalt::geom_lollipop(size = 1.5, color = "#003049") + 
  coord_flip() + 
  facet_wrap(vars(group)) +
  labs(y = glue("Percent of respondents\n(N = {sum(tab$n)})"), 
       x= NULL) +
  hrbrthemes::scale_y_percent(limits = c(0, .5))


p3 = disp %>% 
  group_by(dis_return) %>% 
  tally() %>% 
  drop_na() %>% 
  mutate(pct = n/sum(n)) %>% 
  mutate(group = "Were you able to return home?") %>% 
  ggplot(aes(x = factor(dis_return), y = pct)) + 
  ggalt::geom_lollipop(size = 1.5, color = "#003049") + 
  coord_flip() +
  facet_wrap(vars(group)) +
  labs(y = glue("Percent of respondents\n(N = {length(na.omit(disp$dis_return))})"), 
       x= NULL) +
  hrbrthemes::scale_y_percent(limits = c(0, .6)) + 
  scale_x_discrete(breaks = c(0, 1), labels = c("No", "Yes"))


p4 = disp %>% 
  mutate(dis_returnyr = ifelse(dis_returnyr == 20000, 2000, dis_returnyr)) %>% 
  mutate(time_to_return = dis_returnyr - displace_yr) %>% 
  select(dis_returnyr, displace_yr, time_to_return) %>% 
  mutate(group = "Length of time until return\n(in years)") %>% 
  ggplot(aes(x = time_to_return)) + 
  facet_wrap(vars(group)) +
  geom_histogram(color = "white", fill = "#003049") + 
  labs(x = NULL, y = NULL) + 
  scale_x_continuous(breaks = seq(0, 70, by = 5))


(p3 + p1) / (p4 + p2)

ggsave(here("replication", "figures", "desc-return.pdf"), device = cairo_pdf)





# restitution experiences -------------------------------------------------




# seeks restitution?
p1 = disp %>% 
  group_by(rest_yn) %>% 
  tally() %>% 
  drop_na() %>% 
  mutate(pct = n/sum(n)) %>% 
  mutate(group = "Have you sought restitution?") %>% 
  ggplot(aes(x = factor(rest_yn), y = pct)) + 
  ggalt::geom_lollipop(size = 1.5, color = "#003049") + 
  coord_flip() +
  facet_wrap(vars(group)) +
  labs(y = glue("Percent of respondents\n(N = {length(na.omit(disp$rest_yn))})"), 
       x= NULL) +
  hrbrthemes::scale_y_percent(limits = c(0, .8)) + 
  scale_x_discrete(breaks = c(0, 1), labels = c("No", "Yes"))




# who is reperesneting you?
p3 = disp %>% 
  group_by(con_rep) %>% 
  tally() %>% 
  drop_na() %>% 
  mutate(pct = n/sum(n)) %>% 
  mutate(con_rep = case_when(con_rep == 1 ~ "Public defender", 
                             con_rep == 2 ~ "NGO", 
                             con_rep == 3 ~ "Private lawyer", 
                             con_rep == 4 ~ "No representation", 
                             con_rep == 5 ~ "Family", 
                             con_rep == 97 ~ "Other")) %>% 
  mutate(group = "Who is advocating your case?") %>% 
  ggplot(aes(x = reorder(con_rep, pct), y = pct)) + 
  ggalt::geom_lollipop(size = 1.5, color = "#003049") + 
  coord_flip() +
  facet_wrap(vars(group)) +
  labs(y = glue("Percent of respondents\n(N = {length(na.omit(disp$con_rep))})"), 
       x= NULL) +
  hrbrthemes::scale_y_percent(limits = c(0, .5))


# how fast is restiuiton? 
time = disp %>% 
  select(rest_stage, rest_beginy, rest_endy) %>% 
  # replace na with 2020 for people who haven't finished
  mutate(rest_endy = replace_na(rest_endy, 2020)) %>% 
  # get rid of people who had decisions rejected
  filter(rest_stage != 2) %>% 
  # get rid of people with weird answers
  filter(rest_endy >= 1980, rest_beginy >= 1970) %>% 
  drop_na(rest_beginy) %>% 
  mutate(time_to_rest = rest_endy - rest_beginy) %>% 
  # filter out peopel who gave impossible answers
  filter(time_to_rest >= 0) %>% 
  mutate(clean = case_when(rest_stage == 1 ~ "Request accepted", 
                           rest_stage == 2 ~ "Request rejected", 
                           rest_stage %in% c(3, 6, 7) ~ "Under review", 
                           rest_stage == 4 ~ "Verdict reached,\nno restitution", 
                           rest_stage == 5 ~ "Land restituted", 
                           TRUE ~ NA_character_)) %>% 
  mutate(clean = fct_relevel(clean, "Under review", "Request accepted", 
                             "Verdict reached,\nno restitution", "Land restituted"), 
         group = "Time in restitution process") %>% 
  mutate(still_waiting = ifelse(rest_endy == 2020, "Yes", "No"))





p4 = time %>% 
  ggplot(aes(x = clean, y = time_to_rest, fill = still_waiting)) + 
  ggbeeswarm::geom_quasirandom(width = .4, alpha = .8,
                               shape = 21, size = 4, color = "white") +
  scale_fill_manual(values = c("#003049", "coral")) + 
  theme(legend.position = "bottom") + 
  facet_wrap(vars(group)) + 
  labs(y = NULL, x = glue::glue("In years\n(N = {length(na.omit(time$still_waiting))})"), 
       fill = "Still waiting for restitution?")



(p1 + p3) / (p4)

ggsave(here("replication", "figures", "rest-qual.pdf"), device = cairo_pdf)
